package com.jrtc.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.base.entity.bo.Disease;
import com.jrtc.base.entity.bo.Exerciseprescription;
import com.jrtc.base.entity.bo.Patient;
import com.jrtc.base.entity.vo.LicenseNumExerciseVo;
import com.jrtc.base.entity.vo.RemindVo;
import com.jrtc.service.DiseaseService;
import com.jrtc.service.ExerciseprescriptionService;
import com.jrtc.service.PatientService;
import com.jrtc.service.RemindService;
import org.springframework.web.bind.annotation.*;
import sun.misc.CEFormatException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 提醒表(Remind)表控制层
 */
@RestController
@RequestMapping("remind")
public class RemindController {
    /**
     * 服务对象
     */
    @Resource
    private RemindService remindService;
    @Resource
    private PatientService patientService;
    @Resource
    private ExerciseprescriptionService exerciseprescriptionService;

    /**
     * 设置提醒
     * */
    @PostMapping("/insertRemind")
    public ResultDTO insertRemind(HttpServletRequest request, @RequestBody RemindVo remindVo){
        Patient patient;
        try {
            patient = patientService.getLoginPatient(request);
        } catch (Exception e){
            return null;
        }

        int i;
        try {
            i = remindService.insertRemind(remindVo, patient.getPatientId());
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        if (i > 0){
            return ResultDTOBuilder.success("设置时间成功");
        }
        else {
            return ResultDTOBuilder.failureMsg("设置时间失败");
        }
    }

    /**
     * 患者证件号获取运动处方
     * */
    @PostMapping("/licenseNumExercise")
    public ResultDTO licenseNumExercise(@RequestBody Map<String, Object> map){
        String idCard = map.get("idCard").toString();
        List<LicenseNumExerciseVo> licenseNumExerciseVoList;
        try {
            licenseNumExerciseVoList = patientService.licenseNumExercise(idCard);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        return ResultDTOBuilder.success(licenseNumExerciseVoList);
    }

    /**
     * 运动处方详情
     * */
    @PostMapping("/exerciseDetails")
    public ResultDTO exerciseDetails(@RequestBody Map<String, Object> map){
        Integer id = Integer.parseInt(map.get("id").toString());
        Integer healthplanId = Integer.parseInt(map.get("healthplanId").toString());
//        String idCard = map.get("idCard").toString();
        Exerciseprescription exerciseprescription;
        try {
//            exerciseprescription = exerciseprescriptionService.exerciseDetails(id, idCard);
            exerciseprescription = exerciseprescriptionService.exerciseDetails(id, healthplanId);
        } catch (Exception e){
            return ResultDTOBuilder.failure(e.getMessage());
        }

        exerciseprescription.setCount(5);

        return ResultDTOBuilder.success(exerciseprescription);
    }

    /**
     * 患者的运动处方
     * */
    @PostMapping("/patientExercise")
    public ResultDTO patientExercise(@RequestBody Map<String, Object> map){
        Integer id = Integer.parseInt(map.get("id").toString());
        List<Exerciseprescription> exerciseprescription = exerciseprescriptionService.patientExercise(id);
        return ResultDTOBuilder.success(exerciseprescription);
    }
}